package com.esteel.settlement.pa.service.impl;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.esteel.settlement.entity.TbSettAccount;
import com.esteel.settlement.entity.TbSettAccountChg;
import com.esteel.settlement.entity.TbSettAccountPK;
import com.esteel.settlement.pa.packet.BP1303;
import com.esteel.settlement.pa.packet.PaPacket;
import com.esteel.settlement.pa.packet.RspBP1303;
import com.esteel.settlement.pa.service.PacketProcesser;
import com.esteel.settlement.service.SettAccountChgService;
import com.esteel.settlement.service.SettAccountService;
import com.esteel.settlement.service.SettFundService;

@Service
public class AccountProcesser extends PacketProcesser<BP1303, RspBP1303> {

	@Autowired
	private SettAccountService settAccountService;

	@Autowired
	private SettAccountChgService settAccountChgService;

	@Autowired
	private SettFundService settFundService;

	@Override
	@Transactional
	public RspBP1303 execute(BP1303 packet) {

		RspBP1303 rsp = new RspBP1303();
		rsp.setTranFunc("1303");
		rsp.setServType(PaPacket.RESPONSE);
		rsp.setTransDate(new Date());
		rsp.setQydm(packet.getQydm());
		rsp.setThirdLogNo("1303_"+new Date().getTime());
		
		
		String custAcctId = packet.getCustAcctId();
		TbSettAccount account = settAccountService.findByCustAcctId(custAcctId);

		
		//判断是否存在这个客户
		String thirdCustId = packet.getThirdCustId();
		
		int result = settAccountService.isExistThirdCustId(thirdCustId, packet.getIdCode());
		
		if (result==-1){
			rsp.setRspCode("000001");
			rsp.setRspMsg("账号不存在");
			return rsp;
		}else if(result ==-2){
			rsp.setRspCode("000002");
			rsp.setRspMsg("组织机构代码证不正确");
			return rsp;
		}else if (result==0){
		
			int funcFlag = packet.getFuncFlag();
	
			if (account==null&&(funcFlag == BP1303.MOD || funcFlag == BP1303.DEL)) {
				rsp.setRspCode("000003");
				rsp.setRspMsg("账号不曾绑定");
				return rsp;
			}
	
			if (funcFlag == BP1303.ADD) {

				if (account!=null){
					rsp.setRspCode("000004");
					rsp.setRspMsg("账号已经存在");
				}
				
				account = new TbSettAccount();
				TbSettAccountPK pk = new TbSettAccountPK();
				pk.setCustAcctId(custAcctId);
				pk.setThirdCustId(thirdCustId);
				BeanUtils.copyProperties(packet, account);
				account.setId(pk);
				account.setTranDate(packet.getTransDate());
				
				settAccountService.save(account);
				// 同步账户资金数据
				//settFundService.syncFundInfo(custAcctId);
			}
	
			if (funcFlag == BP1303.MOD) {
				BeanUtils.copyProperties(packet, account);
				TbSettAccountPK pk = new TbSettAccountPK();
				pk.setCustAcctId(custAcctId);
				pk.setThirdCustId(thirdCustId);
				account.setId(pk);
				settAccountService.save(account);

			}
			if (funcFlag == BP1303.DEL) {
				TbSettAccountPK pk = new TbSettAccountPK();
				pk.setCustAcctId(custAcctId);
				pk.setThirdCustId(thirdCustId);
				account.setId(pk);
				this.settAccountService.delete(account);

			}
			//记录账户信息变动记录
			TbSettAccountChg accountChg = new TbSettAccountChg();
			BeanUtils.copyProperties(packet, accountChg);
			accountChg.setTranDate(packet.getTransDate());
			accountChg = settAccountChgService.save(accountChg);
			rsp.setThirdLogNo("1303_"+accountChg.getChgId());
			rsp.setRspCode(PaPacket.SUCCESS);
			return rsp;
		}else{
			rsp.setRspCode("100000");
			rsp.setRspMsg("未知错误");
			return rsp;
		}
	}
	
	

	@Override
	protected String getTranFunc() {
		return "1303";
	}



}
